﻿//===============================================================================
//	LuaScriptManager.cpp
//	lua脚本管理器
//===============================================================================

#include <fstream>

#include "LuaScriptManager.h"
#include "../share/user_define.h"

#include "LuaManager.h"//reloadlei
#include "../../3rd/libjson/json.h"

#include "script_define.h"

const char FileScriptIndex[] = "script/script_index.txt";

//-------------------------------------------------------------------------------
LuaScriptManager::LuaScriptManager(void)
	: m_ptr_count(0)
	, m_game_host_id(0)
{
	_safe_guard_begin;

	for (uint32_t i = 0; i < size_16; ++i)
	{
		m_ptr[i] = nullptr;
	}

	_safe_guard_end;
}


//-------------------------------------------------------------------------------
LuaScriptManager::~LuaScriptManager(void)
{
	std::lock_guard<std::recursive_mutex> locker(m_lua_mutex);
	for (int32_t i = 0; i < m_ptr_count; ++i)
	{
		m_ptr[i]->Exit();
		m_ptr[i] = nullptr;
	}
}

//-------------------------------------------------------------------------------
bool LuaScriptManager::Initialize(int32_t hostId)
{
	_safe_guard_begin;

	printf("Load LuaScriptManager FileScriptIndex start ...\n");

	if (nullptr == FileScriptIndex)
	{
		printf("Load LuaScriptManager FileScriptIndex is nullptr end ...\n");

		return false;
	}

	m_game_host_id = hostId;
	//	载入文件

	std::ifstream _fin;
	_fin.open(FileScriptIndex, std::ios::in);

	int32_t _idx = 0;

	if ( _fin.is_open() )
	{
		char _tmp[1024];
		while (!_fin.eof())
		{
			memset(_tmp, 0, 1024);
			_fin.getline(_tmp, 1024);

			if ((0 == strlen(_tmp)) || ('#' == _tmp[0]))
			{
				continue;
			}

			std::stringstream _tmp_ss(_tmp);
			m_script_info[_idx].Initialize(_tmp_ss);
			++_idx;
		}
		_fin.close();
	}

	m_script_count = _idx;

	printf("Load LuaScriptManager FileScriptIndex end ...\n");

	return true;

	_safe_guard_end;
	
	return false;
}


//-------------------------------------------------------------------------------
char* LuaScriptManager::GetFileNameByScriptId( uint32_t scriptID )
{
	_safe_guard_begin;

	for ( int32_t i = 0; i < m_script_count; ++i )
	{
		if( scriptID == m_script_info[i].m_script_id )
		{
			return m_script_info[i].m_filename;
		}
	}

	LOG_ERROR("default","GetFileNameByScriptId fail: scriptID=%d", scriptID);
	return nullptr;
	
	_safe_guard_end;
	return nullptr;
}

//-------------------------------------------------------------------------------
bool LuaScriptManager::UpdateToLuaManager(lua_manager_ptr& lmp)
{
	_safe_guard_begin;

	std::lock_guard<std::recursive_mutex> locker(m_lua_mutex);

	if (m_ptr_count >= size_16)
	{
		printf("lua script manager full ! size is size_16.\n");
		return false;
	}	
	
	m_ptr[m_ptr_count] = std::make_shared<LuaManager>();

	lmp = m_ptr[m_ptr_count];

	lmp->SetIndex(m_ptr_count);
	++m_ptr_count;


	lmp->Initialize();

	for (int32_t i = 0; i < m_script_count; ++i)
	{
		if (strlen(m_script_info[i].m_filename) > 0)
		{
			lmp->LoadScript(m_script_info[i].m_filename);
		}
	}

//	Json::Value db = ReadJsonFile("./config/dbConfig.json");
//	std::string dbString = JsonWriter(db);

	// 相应配置文件
	Json::Value config = ReadJsonFile("./config/HostsConfig.json");

	// 
	Json::Value db;
	db.append(config["accdb"]);
	db.append(config["gamedb"]);
	db.append(config["sidb"]);

	//Json::Value redis;
	//redis.append(config["redis"]);

	std::string dbString = JsonWriter(db);
	std::string redisString = JsonWriter(config["redis"]); 
	std::string webredisString = JsonWriter(config["webredis"]);

	// 初始化脚本
	lmp->RunScriptFunction(LUA_BaseConfigScript, LUA_InitLuaScriptEnvironment, m_game_host_id, dbString.c_str(), redisString.c_str(), webredisString.c_str());

	return true;

	_safe_guard_end;
	
	return false;
}

//-------------------------------------------------------------------------------
bool LuaScriptManager::reload(uint32_t scriptID)
{
	_safe_guard_begin;

	char * pfile = GetFileNameByScriptId(scriptID);
	if (nullptr == pfile)
	{
		Clear();
		Initialize(m_game_host_id);
	}
	
	pfile = GetFileNameByScriptId(scriptID);
	if (nullptr == pfile)
	{
		printf("script id [%d] no exists !\n", scriptID);
		return false;
	}
	
	{
		std::lock_guard<std::recursive_mutex> locker(m_lua_mutex);
		for (int32_t i = 0; i < m_ptr_count; ++i)
		{
			m_ptr[i]->LoadScript(pfile);
		}
	}

	printf("script id [%d] reload ok !\n", scriptID);
	return true;

	_safe_guard_end;

	return false;
}

//-------------------------------------------------------------------------------
void LuaScriptManager::Clear(void)
{
	_safe_guard_begin;

	for (uint32_t i = 0; i < size_1024; ++i)
	{
		m_script_info[i].Clear();
	}

	_safe_guard_end;
}
